<?php

/*
*
* BEGIN fetchpathitems.php
*
* 																											
* Silent Bob Fetch Path Items
* @Description: Fetches files or folders contained within the specified path
* @Authors: Salman, Wits University
* @Authors: Monty Furmie, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 8 September 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

class fetchpathitems extends baseClass
{
	public $cm = array();
	
	public function __construct($path, $filetype, $type = "file")
	{
		switch ($type)
		{
			case "file":
				if ($this->fetchFileNames($path, $filetype)->status == 1)
				{
					$this->cm = $this->fetchFileNames($path, $filetype)->response;
				}
			break;
			
			case "folder":
				if ($this->fetchFolderNames($path)->status == 1)
				{
					$this->cm = $this->fetchFolderNames($path)->response;
				}
			break;
		}
		
		$standardResponse->status = 1;
		$standardResponse->response = $this->cm;
		return $standardResponse;
	}
	
	/*******************************************************************************************************************************************************************
	* BEGIN Fetch Files Function (fetches all files in a folder and returns an array
	/******************************************************************************************************************************************************************/
	
	/* TODO: MOVE THIS INTO IT'S OWN BASE CLASS!! */
	private function fetchFileNames($path, $fileType)
	{
		$filesArray = array();
		if ($handle = opendir($path))
		{
			while (false !== ($file = readdir($handle)))
			{
				if ($file != "." && $file != ".." && (preg_match("/.$fileType/i", $file) == 1) && (substr($file, 0, 1) != "_"))
				{
					$filesArray[] = $file;
				}
			}
			closedir($handle);
		}
		sort($filesArray);
		$standardResponse->status = 1;
		$standardResponse->response = $filesArray;
		return $standardResponse;
	}
	/*******************************************************************************************************************************************************************
	* END Fetch Files Function
	/******************************************************************************************************************************************************************/
	
	
	/*******************************************************************************************************************************************************************
	* BEGIN Fetch Folder Function (fetches all files in a folder and returns an array
	/******************************************************************************************************************************************************************/
	/* TODO: MOVE THIS INTO IT'S OWN BASE CLASS!! */
	private function fetchFolderNames($path)
	{
		$filesArray = array();
		if ($handle = opendir($path))
		{
			while (false !== ($file = readdir($handle)))
			{
				if ($file != "." && $file != ".." && (substr($file, 0, 1) != "_"))
				{
					$filesArray[] = $path . "/" . $file;
				}
			}
			closedir($handle);
		}
		sort($filesArray);
		$standardResponse->status = 1;
		$standardResponse->response = $filesArray;
		return $standardResponse;
	}
	/*******************************************************************************************************************************************************************
	* END Fetch Files Function
	/******************************************************************************************************************************************************************/
}

/*
*
* END fetchpathitems.php
*
*/
?>